package com.naiterui.ehp.bp.support.filter;

import com.naiterui.ehp.bp.support.security.HeaderConstant;
import com.naiterui.ehp.bp.support.security.SettingConfig;
import com.naiterui.ehp.bp.support.utils.JwtHelper;
import com.naiterui.ehp.bp.support.utils.JwtUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Optional;

/**
 * 微信H5请求，jwt token 处理获取patientId过滤器
 * 解决历史controller中参数接受patientId问题
 * 若重构鉴权gateway，需要考虑废弃
 *
 * @author gaoliangliang
 * @date 2019-07-09 15:09
 * @since 1.0.0
 */
@Component
@Slf4j
public class WxPatientParamFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
                                    FilterChain filterChain) throws ServletException, IOException {

        String origin = request.getHeader(HeaderConstant.HEADER_ORIGIN);
        String token = request.getHeader(SettingConfig.TOKEN);
        String headToken = request.getHeader(SettingConfig.TOKEN_HEADER);
        if ((HeaderConstant.HEADER_ORIGIN_PATIENT_WX + "").equals(origin) && StringUtils.isNotBlank(token)) {
            ParameterRequestWrapper requestWrapper = new ParameterRequestWrapper(request);
            JwtUser jwtUser = JwtHelper.verifyToken(token);
            Long patientId = Optional.ofNullable(jwtUser).map(JwtUser::getUserId).orElse(null);
            log.debug("获取用户token :{} patientId:{}", token, patientId);
            requestWrapper.addParameter(SettingConfig.PATIENT_ID, patientId);
            requestWrapper.addParameter(SettingConfig.TOKEN, token);
            filterChain.doFilter(requestWrapper, response);
        } else if ((HeaderConstant.HEADER_ORIGIN_DEPUTY_WX + "").equals(origin) && StringUtils.isNotBlank(token)) {
            ParameterRequestWrapper requestWrapper = new ParameterRequestWrapper(request);
            JwtUser jwtUser = JwtHelper.verifyToken(token);
            Long deputyId = Optional.ofNullable(jwtUser).map(JwtUser::getUserId).orElse(null);
            requestWrapper.addParameter(SettingConfig.DEPUTY_ID, deputyId);
            requestWrapper.addParameter(SettingConfig.TOKEN, token);
            filterChain.doFilter(requestWrapper, response);
        } else if ((HeaderConstant.HEADER_ORIGIN_PATIENT_APP + "").equals(origin) && StringUtils.isNotBlank(headToken)) {
            JwtUser jwtUser = JwtHelper.parseToken(headToken);
            Long patientId = Optional.ofNullable(jwtUser).map(JwtUser::getUserId).orElse(null);
            Integer userType = Optional.ofNullable(jwtUser).map(JwtUser::getUserType).orElse(null);
            log.info("患者小程序请求，资源上下文中获取到当前登录患者用户ID {}", patientId);
            ParameterRequestWrapper requestWrapper = new ParameterRequestWrapper(request);
            log.debug("获取小程序患者用户token :{} patientId:{}", headToken, patientId);
            requestWrapper.addParameter(SettingConfig.PATIENT_ID, patientId);
            requestWrapper.addParameter(SettingConfig.USER_ID, patientId);
            requestWrapper.addParameter(SettingConfig.USER_TYPE, userType);
            filterChain.doFilter(requestWrapper, response);
        } else {
            filterChain.doFilter(request, response);
        }
    }
}
